<?php

namespace App\Admin\Controllers;

use App\Models\CollPosition;
use App\Models\ProductAttrKey;
use Encore\Admin\Controllers\AdminController;
use App\Admin\Actions\Post\Restore;
use App\Admin\Actions\Post\DeletePosition;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Table;
use Illuminate\Support\Facades\Request;


class CollPositionController extends AdminController
{
    protected $title = '工种列表';

    protected static $baseImg = 'https://liande.oss-cn-beijing.aliyuncs.com';

    protected function grid()
    {
        $pid = Request::get('pid', 0);
        $grid = new Grid(new CollPosition());
        $grid->model()->where('pid', $pid)->orderBy('sort', 'asc');

        $grid->column('id', __('Id号'));
        $grid->column('sort', __('排序'))->sortable();
        $grid->column('name', __('工种名称'))->modal('子工种', function ($model) {

            $positions = $model->child()->get()->map(function ($position) {
                return $position->only(['id', 'sort', 'name', 'cid']);
            });

            $positions = $positions->toArray();
            $sortArr = array_column($positions, 'sort');
            array_multisort($sortArr, SORT_ASC, $positions);

            return new Table(['ID号', '排序', '工种名称', '工种编码'], $positions);
        });

        $grid->column('img1', __('工种图'))->image(self::$baseImg, 100, 100);
        $grid->column('cid', '工种编码');

        $grid->column('status', __('状态'))->using(['-1' => '弃用', '1' => '正常']);

        $grid->actions(function ($actions) {
            $actions->disableDelete();
            $actions->add(new DeletePosition());
        });

        return $grid;
    }

    protected function detail($id)
    {
        $show = new Show(CollPosition::findOrFail($id));

        $show->field('id', __('Id号'));
        $show->field('sort', __('排序'));
        $show->field('name', __('工种名称'));
        $show->field('img1', __('工种图'))->image();
        $show->field('cid', __('工种编码'));
        $show->field('status', __('状态'))->using(['-1' => '弃用', '1' => '正常']);

        $show->panel()
            ->tools(function ($tools) {
                $tools->disableDelete();
            });
        $cid = CollPosition::where('id', $id)->value('cid');
        $sonNum = CollPosition::where('pid', $cid)->count();
        if ($sonNum > 0) {
            $show->child('子分类', function ($vals) {
                $vals->resource('/admin/coll_position');
                $vals->id('ID号');
                $vals->sort('排序');
                $vals->name('工种名称');
                $vals->img1('工种图')->image();
                $vals->cid('工种编码');

                $vals->actions(function ($actions) {
                    $actions->disableDelete();
                    $actions->add(new DeletePosition());
                });
            });
        }

        return $show;
    }

    protected function form()
    {
        $form = new Form(new CollPosition());

        if ($form->isCreating()) {
            $pid = Request::input('pid', 0);
            if ($pid == 0) {
                $pname = '顶级分类';
                $grade = 1;
            } else {
                $pname = CollPosition::where('cid', $pid)->value('name');
                $grade = 2;
            }

            $options = [
                $pid => $pname,
            ];
            $form->select('pid', '父级分类')->options($options)->default($pid)->rules('required', [
                'required' => '必须选择父级分类',
            ]);

            $form->number('grade')->value($grade);
        }

        $form->text('sort', __('排序'))->required();
        $form->text('name', __('工种名称'))->required();
        $form->image('img1', __('工种图'))->required();
        $form->number('cid', __('工种编码'))->required();

        $form->radio('status', __('状态'))->options(['-1' => '弃用', '1' => '正常'])->default('1');

        $form->tools(function (Form\Tools $tools) {
            // 去掉`删除`按钮
            $tools->disableDelete();
        });

        $form->footer(function ($footer) {
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
        });

        return $form;
    }
}